<?php
/*
 * @Author: lokei
 * @Date: 2022-09-19 13:45:10
 * @LastEditors: lokei
 * @LastEditTime: 2022-11-15 14:38:46
 * @Description: 
 */
namespace App\Http\Controllers\Cms\Article;

use App\Common\Tools\ResultTool;
use App\Http\Controllers\Controller;
use App\Models\Cms\Article\ArticleModel;
use App\Models\Cms\Article\ArticleRecommendModel;
use Illuminate\Http\Request;

class ArticleController extends Controller
{
	public function get(Request $request)
	{
		$article = ArticleModel::where('id', '=', $request->input('id'))->first();
		$res = ResultTool::success();
		$res['data'] = $article;
		return $res;
	}
	public function list(Request $request)
	{
		if (!is_null($request->input('mode')) && $request->input('mode') == 'recommend') {
			$recommend = ArticleRecommendModel::where('scene', '=', $request->input('scene'))->first();
			if($recommend != null) {
				$ids = explode(';', $recommend->article_list);
				$items = ArticleModel::whereIn('id', $ids)->get();
			}
        } else {
			$items = ArticleModel::get();
		}
		$data['items'] = $items;
		$data['count'] = count($items);
		$res = ResultTool::success();
		$res['data'] = $data;
		return $res;
	}
	public function update(Request $request)
	{
		$status = '0';
		$article = ArticleModel::where('id', '=', $request->input('id'))->first();
		$article->title = $request->input('title');
		$article->img_display = $request->input('img_display');
		$article->content = $request->input('content');
		$article->status = $request->input('status');
		$article->save();
		return ResultTool::success();
	}
	public function create(Request $request)
	{
		$article = new ArticleModel();
		$article->title = $request->input('title');
		$article->img_display = $request->input('img_display');
		$article->content = $request->input('content');
		$article->createtime = date('Y-m-d H:i:s', time());
		$article->status = $request->input('status');
		$article->save();
		return ResultTool::success();
	}
	public function delete(Request $request)
	{
		ArticleModel::where('id', '=', $request->input('id'))->delete();
		return ResultTool::success();
	}
}
